<template>
    <div class="post-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item :label="$t('岗位编码')">
                    <el-input class="w-[280px]" v-model="queryParams.code" clearable @keyup.enter="resetPage" />
                </el-form-item>
                <el-form-item :label="$t('岗位名称')">
                    <el-input class="w-[280px]" v-model="queryParams.name" clearable @keyup.enter="resetPage" />
                </el-form-item>
                <el-form-item :label="$t('岗位状态')">
                    <el-select class="w-[280px]" v-model="queryParams.status">
                        <el-option :label="$t('全部')" value />
                        <el-option :label="$t('正常')" :value="1" />
                        <el-option :label="$t('停用')" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">{{ $t('查询') }}</el-button>
                    <el-button @click="resetParams">{{ $t('重置') }}</el-button>
                    <export-data class="ml-2.5" :fetch-fun="jobsLists" :params="queryParams" :page-size="pager.size" />
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['dept/jobs/add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    {{ $t('新增') }}
                </el-button>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column :label="$t('岗位编码')" prop="code" min-width="100" />
                <el-table-column :label="$t('岗位名称')" prop="name" min-width="100" />
                <el-table-column :label="$t('排序')" prop="sort" min-width="100" />
                <el-table-column :label="$t('备注')" prop="remark" min-width="100" show-overflow-tooltip />
                <el-table-column :label="$t('添加时间')" prop="create_time" min-width="180" />
                <el-table-column :label="$t('状态')" prop="status" min-width="100">
                    <template #default="{ row }">
                        <el-tag class="ml-2" :type="row.status ? '' : 'danger'">
                            {{ row.status_desc }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button v-perms="['dept/jobs/edit']" type="primary" link @click="handleEdit(row)">
                            {{ $t('编辑') }}
                        </el-button>
                        <el-button v-perms="['dept/jobs/delete']" type="danger" link @click="handleDelete(row.id)">
                            {{ $t('删除') }}
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="post">
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
import { jobsDelete, jobsLists } from '@/api/dept/post'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    code: '',
    name: '',
    status: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: jobsLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm(t('确定要删除?'))
    await jobsDelete({ id })
    getLists()
}

getLists()
</script>
